package pl.iachoo.ctl.tabory.dao.ibatis.services;

import java.io.Reader;

import org.apache.log4j.Logger;

import pl.iachoo.ctl.tabory.dao.api.services.ICompositionService;
import pl.iachoo.ctl.tabory.dao.api.services.IUserRoleService;
import pl.iachoo.ctl.tabory.dao.api.services.IUserService;
import pl.iachoo.ctl.tabory.dao.ibatis.services.impl.CompositionDaoServiceImpl;
import pl.iachoo.ctl.tabory.dao.ibatis.services.impl.UserDaoServiceImpl;
import pl.iachoo.ctl.tabory.dao.ibatis.services.impl.UserRoleDaoServiceImpl;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class ServiceManager {

	private final static Logger logger = Logger.getLogger(ServiceManager.class);

	private static IUserRoleService userRoleService;
	private static IUserService userService;
	private static ICompositionService compositionService;

	static {
		try {
			final Reader reader = Resources
					.getResourceAsReader("iBatis/ibatis-config.xml");
			SqlMapClient sqlMapClient = SqlMapClientBuilder
					.buildSqlMapClient(reader);
			reader.close();
			userRoleService = new UserRoleDaoServiceImpl(sqlMapClient);
			userService = new UserDaoServiceImpl(sqlMapClient);
			compositionService = new CompositionDaoServiceImpl(sqlMapClient);
		} catch (final Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public static IUserRoleService getUserRoleService() {
		return userRoleService;
	}

	public static IUserService getUserService() {
		return userService;
	}

	public static ICompositionService getCompositionService() {
		return compositionService;
	}

}
